Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '请选择报名必填内容',
    isName: 1,
    isTelphone: 1,
    isSex: 0,
    isWechatNum: 0,
    isIdcard: 0,
    isCusromizeFlag: 0,
    isCusromize: '',
    maxLength: 6,
    currentLength: 0
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  itemTap(e) {
    let field = e.currentTarget.dataset.field;
    this.toggle(field, this.data[field] == 1 ? 0 : 1);
    if (field == 'isCusromizeFlag' && this.data[field] == 0) {
      this.setData({
        isCusromize: ''
      })
    }
  },
  save() {
    this.setData({
      isCusromize: this.data.isCusromize.trim()
    });
    this.setData({
      currentLength: this.data.isCusromize.length
    });
    if (this.data.isCusromizeFlag > 0 && this.data.isCusromize == '') {
      wx.showToast({
        title: '请输入自定义名称',
        icon: 'none',
        duration: 2000
      })
    }else{
      let pages = getCurrentPages(),
        prevPage = pages[pages.length - 2];
      prevPage.toggle('act.isName', this.data.isName);
      prevPage.toggle('act.isTelphone', this.data.isTelphone);
      prevPage.toggle('act.isSex', this.data.isSex);
      prevPage.toggle('act.isWechatNum', this.data.isWechatNum);
      prevPage.toggle('act.isIdcard', this.data.isIdcard);
      prevPage.toggle('act.isCusromize', this.data.isCusromize);
      prevPage.toggle('addType', this.setText());

      wx.navigateBack({
        delta: 1
      })
    }
  },
  setText() {
    let count = this.data.isSex + this.data.isWechatNum + this.data.isIdcard + this.data.isCusromizeFlag,
      text = '姓名、手机号';
    if (count > 0) {
      text += '等';
    }
    return text;
  },
  isCusromizeInput(e) {
    var isCusromize = e.detail.value,
      currentLength = e.detail.value.length;
    if (currentLength > this.data.maxLength) {
      wx.showToast({
        title: '输入的文字数量已达到上限',
        icon: 'none',
        duration: 2000
      })
      return this.data.isCusromize;
    }
    this.setData({
      isCusromize: isCusromize,
      currentLength: currentLength
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.isName && options.isName != '') {
      this.setData({
        isName: options.isName,
      })
    }
    if (options.isTelphone && options.isTelphone != '') {
      this.setData({
        isTelphone: options.isTelphone,
      })
    }
    if (options.isSex && options.isSex != '') {
      this.setData({
        isSex: options.isSex,
      })
    }
    if (options.isWechatNum && options.isWechatNum != '') {
      this.setData({
        isWechatNum: options.isWechatNum,
      })
    }
    if (options.isIdcard && options.isIdcard != '') {
      this.setData({
        isIdcard: options.isIdcard,
      })
    }
    if (options.isCusromize && options.isCusromize != '') {
      this.setData({
        isCusromize: options.isCusromize,
        isCusromizeFlag: options.isCusromize != '' ? 1 : 0,
        currentLength: options.isCusromize.length
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  }
})